<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AHDA Integration</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Assistant:wght@200..800&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: Assistant, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #fff;
            padding: 3rem;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            text-align: center;
            max-width: 400px;
            width: 100%;
        }

        h1 {
            margin-bottom: 2rem;
        }

        .logo-container {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-bottom: 2rem;
        }

        .logo-container img {
            width: 80px;
            height: 80px;
            object-fit: contain;
        }

        input[type="text"],
        button {
            width: 100%;
            padding: 10px;
            margin-bottom: 1rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        button {
            background-color: #000;
            color: #fff;
            border: none;
            cursor: pointer;
            font-size: 1rem;
        }

        button:hover {
            background-color: #333;
        }

        p {
            margin-top: 1rem;
        }

        .plugin-text {
            font-size: 1.2rem;
            font-weight: bold;
            margin-bottom: 1rem;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="logo-container">
            <img src="https://avatars.githubusercontent.com/u/162546372" alt="OMI Logo" />
            <img src="https://avatars.githubusercontent.com/u/181646370" alt="AHDA Logo" />
        </div>
        <p class="plugin-text">AHDA Integration Plugin</p>
        <h1>AHDA Integration</h1>
        Install https://github.com/ActuallyAdvanced/OMI-AHDA first
        <br>
        <form id="ahda-form">
            <input type="text" id="url" placeholder="Enter AHDA URL" required>
            <input type="text" id="os" placeholder="Enter Operating System" required>
            <button type="submit">Save Configuration</button>
        </form>
        <p id="response-message"></p>
    </div>

    <script>
        // Extract the uid from the URL query parameters
        const urlParams = new URLSearchParams(window.location.search);
        const uid = urlParams.get('uid');

        const url = urlParams.get('url');
        const os = urlParams.get('os');

        if (!uid) {
            document.getElementById('response-message').textContent = "UID is missing. Please check the URL.";
            document.getElementById('response-message').style.color = 'red';
        }

        document.getElementById('ahda-form').addEventListener('submit', async function (event) {
            event.preventDefault();

            const url = document.getElementById('url').value;
            const os = document.getElementById('os').value;

            const response = await fetch('/ahda/configure', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: new URLSearchParams({ uid, url, os })
            });

            const result = await response.json();
            const messageElement = document.getElementById('response-message');
            if (response.ok) {
                messageElement.textContent = result.message;
                messageElement.style.color = 'green';
            } else {
                messageElement.textContent = result.detail || 'An error occurred. Please try again.';
                messageElement.style.color = 'red';
            }
        });
    </script>
</body>

</html>